<template>
  <div class="time-record-container">
    <el-card>
      <div class="header-title">工时记录查看</div>
      <el-table :data="tableData" border stripe style="width: 100%; margin-top: 20px;">
        <el-table-column prop="positionName" label="岗位名称" align="center" />
        <el-table-column prop="date" label="日期" align="center" />
        <el-table-column prop="startTime" label="开始时间" align="center" />
        <el-table-column prop="endTime" label="结束时间" align="center" />
        <el-table-column prop="description" label="工作描述" align="center" />
        <el-table-column label="状态" align="center">
          <template #default="scope">
            <el-tag :type="statusType(scope.row.status)">
              {{ statusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-wrapper">
        <el-pagination
          background
          layout="prev, pager, next, jumper, ->, total"
          :total="total"
          :page-size="pageSize"
          :current-page="pageNum"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const tableData = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(8);
const loading = ref(false);

const statusText = (status) => {
  switch (status) {
    case 0: return '未审核';
    case 1: return '已通过';
    case 2: return '未通过';
    default: return '未知';
  }
};
const statusType = (status) => {
  switch (status) {
    case 0: return 'info';
    case 1: return 'success';
    case 2: return 'danger';
    default: return '';
  }
};

const fetchData = async () => {
  loading.value = true;
  try {
    const token = localStorage.getItem('token');
    if (!token) {
      ElMessage.error('请先登录');
      return;
    }
    const res = await axios.get('/api/student/work-time-records', {
      params: {
        pageNum: pageNum.value,
        pageSize: pageSize.value
      },
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    if (res.data.code === 0) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    } else {
      ElMessage.error(res.data.msg || '加载失败');
    }
  } catch (e) {
    ElMessage.error('请求失败: ' + e.message);
  } finally {
    loading.value = false;
  }
};

const handlePageChange = (val) => {
  pageNum.value = val;
  fetchData();
};

onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.time-record-container {
  padding: 24px;
}
.header-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #222;
}
.pagination-wrapper {
  margin-top: 20px;
  text-align: right;
}
</style>
